package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.service.AdminTopicService;
import com.cskaoyan.market.util.MybatisUtil;
import com.cskaoyan.market.vo.TopicGoodVo;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.*;

public class AdminTopicServiceImpl implements AdminTopicService {
    @Override
    public List<MarketTopic> list(String page, String limit, String title, String subtitle, String sort, String order) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        criteria.andDeletedEqualTo(false);
        marketTopicExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<MarketTopic> marketTopicList = mapper.selectByExampleWithBLOBs(marketTopicExample);
        session.commit();
        session.close();
        return marketTopicList;

    }

    @Override
    public MarketTopic insert(MarketTopic marketTopic) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        mapper.insert(marketTopic);
        session.commit();
        session.close();
        return marketTopic;
    }

    @Override
    public Map<String, Object> read(Integer id) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
        Integer[] goodsId = marketTopic.getGoods();
        MarketGoodsMapper goodsMapper = session.getMapper(MarketGoodsMapper.class);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIdIn(Arrays.asList(goodsId));
        List<MarketGoods> marketGoodsList = goodsMapper.selectByExample(marketGoodsExample);
        List<TopicGoodVo> topicGoodVoList = new ArrayList<>();
        for (MarketGoods marketGood : marketGoodsList) {
            topicGoodVoList.add(new TopicGoodVo(marketGood.getId(),marketGood.getName(),marketGood.getBrief(), marketGood.getPicUrl(),
                    marketGood.getIsNew(),marketGood.getIsHot(),marketGood.getCounterPrice(),marketGood.getRetailPrice()));
        }
        //放入到
        Map<String, Object> map = new HashMap<>();
        map.put("topic", marketTopic);
        map.put("goodsList", topicGoodVoList);
        session.commit();
        session.close();
        return map;



    }

    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        Integer id = marketTopic.getId();
        criteria.andIdEqualTo(id);
        mapper.updateByExampleSelective(marketTopic, marketTopicExample);
        marketTopic = mapper.selectByPrimaryKey(id);
        session.commit();
        session.close();
        return marketTopic;

    }

    @Override
    public boolean delete(MarketTopic marketTopic) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        int delete = mapper.deleteByPrimaryKey(marketTopic.getId());
        session.commit();
        session.close();
        if (delete == 1) {
            return true;
        }return false;
    }

    @Override
    public boolean deleteLogically(MarketTopic marketTopic) {
        SqlSession session = MybatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        Integer id = marketTopic.getId();
        int delete = mapper.logicalDeleteByPrimaryKey(id);
        session.commit();
        session.close();
        if (delete == 1) {
            return true;
        }return false;
    }
}
